package net.sansi.v3correctserver.service.takeshowphoto;

import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.entity.image.RecImage;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.entity.space.CorrectCnf;
import net.sansi.v3correctserver.service.camera.Camera;
import net.sansi.v3correctserver.service.camera.CameraService;
import net.sansi.v3correctserver.service.show.ShowService;
import net.sansi.v3correctserver.service.space.ProjectSpace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.file.Path;

/**
 * Created by Fang
 * create time : 2025/2/11
 * IDE : IntelliJ IDEA
 * <p>
 * 服务
 */

@Service
public class TakeShowPhotoService {

    @Resource
    private ShowService showService;
    @Resource
    private CameraService cameraService;

    private boolean isReusePhoto() {
        return AppHelper.projectSpace().getCorrectCnf().getReusePhoto();
    }

    private boolean isSkipTakePhoto(Path savePath) {
        return savePath.toFile().exists() && isReusePhoto();
    }

    /**
     * 拍摄黑色背景相片
     *
     * @param params   相机参数
     * @param savePath 保存路径
     * @return 保存路径
     */
    public Path takeBlack(Camera.Params params, Path savePath) {
        if (isSkipTakePhoto(savePath)) return savePath;
        ShowService.ShowClose showClose = showService.showBlack();
        Path path = cameraService.takePhoto(params, savePath);
        showClose.close();
        return path;
    }

    /**
     * 拍摄黑色背景相片
     *
     * @param params   相机参数
     * @param savePath 保存路径
     * @return 相片
     */
    public RecImage takeBlack2Image(Camera.Params params, Path savePath) {
        Path path = takeBlack(params, savePath);
        return RecImage.load(path);
    }

    /**
     * 拍摄围栏识别用相片
     *
     * @param area     区域
     * @param colorIdx 颜色id
     * @param savePath 保存路径
     * @return 返回保存路径
     */
    public Path takeFence(CorrectArea area, int colorIdx, Path savePath) {
        if (isSkipTakePhoto(savePath)) return savePath;
        ProjectSpace projectSpace = AppHelper.projectSpace();
        CorrectCnf correctCnf = projectSpace.getCorrectCnf();
        ShowService.ShowClose showClose = showService.show(area, 0, 0, 0, 0);
        Path path = cameraService.takePhoto(correctCnf.getCameraParams().get(colorIdx), savePath);
        showClose.close();
        return path;
    }

    /**
     * 拍摄围栏识别用相片
     *
     * @param area     区域
     * @param colorIdx 颜色id
     * @param savePath 保存路径
     * @return 加载后的图片对象
     */
    public RecImage takeFence2Image(CorrectArea area, int colorIdx, Path savePath) {
        Path path = takeFence(area, colorIdx, savePath);
        return RecImage.load(path);
    }

    /**
     * 打屏+拍照
     *
     * @param area     区域
     * @param params   相机参数
     * @param savePath 保存路径
     * @return 保存路径
     */
    public Path take(CorrectArea area, int bright, int colorIdx, int mode, int span, Camera.Params params,
                     Path savePath, int ofx, int ofy) {
        if (isSkipTakePhoto(savePath)) return savePath;
        ShowService.ShowClose showClose = showService.show(area, bright, colorIdx, mode, span, ofx, ofy);
        Path path = cameraService.takePhoto(params, savePath);
        showClose.close();
        return path;
    }

    /**
     * 打屏+拍照
     *
     * @param area     区域
     * @param params   相机参数
     * @param savePath 保存路径
     * @return 加载后的图片对象
     */
    public RecImage take2Image(CorrectArea area, int bright, int colorIdx, int mode, int span, Camera.Params params,
                               Path savePath, int ofx, int ofy) {
        Path path = take(area, bright, colorIdx, mode, span, params, savePath, ofx, ofy);
        return RecImage.load(path);
    }

    /**
     * 拍摄亮度相片
     *
     * @param area      区域
     * @param colorIdx  颜色索引
     * @param ofx       x 偏移
     * @param ofy       y 偏移
     * @param lowBright 是否低亮度
     * @return 保存路径
     */
    public Path takeBright(CorrectArea area, int colorIdx, int ofx, int ofy, boolean lowBright) {

        ProjectSpace projectSpace = AppHelper.projectSpace();
        CorrectCnf correctCnf = projectSpace.getCorrectCnf();
        Path savePath = projectSpace.brightPhoto(area.getAreaId(), colorIdx, ofx, ofy, lowBright);
        if (isSkipTakePhoto(savePath)) return savePath;
        int brightness = correctCnf.getCameraParams().get(colorIdx).getBrightness();
        double kDecayFactor = correctCnf.getKDecayFactor();
        if (lowBright) brightness = (int) (brightness * kDecayFactor);
        int span = correctCnf.getShowSpan();
        return take(area, brightness, colorIdx, 0, span, correctCnf.getCameraParams().get(colorIdx), savePath, ofx,
                ofy);
    }

    /**
     * 拍摄亮度相片
     *
     * @param area      区域
     * @param colorIdx  颜色索引
     * @param ofx       x 偏移
     * @param ofy       y 偏移
     * @param lowBright 是否低亮度
     * @return 相片对象
     */
    public RecImage takeBright2Image(CorrectArea area, int colorIdx, int ofx, int ofy, boolean lowBright) {
        Path path = takeBright(area, colorIdx, ofx, ofy, lowBright);
        return RecImage.load(path);
    }


}
